package com.ea.admin.controller.other;

import com.ea.admin.pojo.param.file.*;
import com.ea.admin.pojo.param.report.GetUploadListParam;
import com.ea.admin.pojo.param.report.MergeParam;
import com.ea.admin.pojo.vo.file.FileInfoVo;
import com.ea.admin.service.FileService;
import com.ea.common.log.LogAnnotation;
import com.ea.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;


@RestController
@Api(tags = "上传文件")
@RequestMapping("/admin/file")
public class FileController {
    @Resource
    FileService fileService;
    @Resource
    HttpServletResponse response;

    @Transactional
    @Operation(summary = "获得chunk的大小")
    @PostMapping("/getChunkSize")
    public Result<Integer> getChunkSize() {
        return Result.success(fileService.CHUNK_SIZE);
    }

    @Transactional
    @Operation(summary = "获得文件信息")
    @PostMapping("/getFileInfo")
    @ApiImplicitParam(dataType = "GetFileInfoParam", name = "getFileInfoParam", value = "文件的md5", readOnly = true, required = true)
    public Result<FileInfoVo> getFileInfo(@RequestBody @Valid GetFileInfoParam getFileInfoParam) {
        FileInfoVo result = fileService.getFileInfo(
                getFileInfoParam.getMd5()
        );
        return Result.success(result);
    }

    @Transactional
    @Operation(summary = "上传文件")
    @PostMapping("/uploadFile")
    @ApiImplicitParam(dataType = "UploadFileParam", name = "uploadVideoParam", value = "上传文件", readOnly = true, required = true)
    public Result<String> uploadFile(@Valid UploadFileParam uploadVideoParam) {
        fileService.uploadFile(
                uploadVideoParam.getFile(),
                uploadVideoParam.getMd5(),
                uploadVideoParam.getType()
        );
        return Result.success(null);
    }

    @Transactional
    @Operation(summary = "下载文件")
    @PostMapping("/downloadFile")
    @ApiImplicitParam(dataType = "DownloadFileParam", name = "downloadFileParam", value = "下载文件", readOnly = true, required = true)
    //请求匹配到方法
    public void downloadFile(@RequestBody @Valid DownloadFileParam downloadFileParam) {
        fileService.downloadFile(
                response,
                downloadFileParam.getMd5()
        );
    }

    @Transactional
    @Operation(summary = "分块上传文件")
    @PostMapping("/uploadChunk")
    @ApiImplicitParam(dataType = "UploadChunkParam", name = "uploadChunkParam", value = "上传视频的分块参数", readOnly = true, required = true)
    @LogAnnotation(module = "FileController", operation = "分块上传文件")
    public Result uploadChunk(@Valid UploadChunkParam uploadChunkParam) {
        fileService.uploadChunk(
                uploadChunkParam.getChunk(),
                uploadChunkParam.getIndex(),
                uploadChunkParam.getTotalChunks(),
                uploadChunkParam.getMd5(),
                uploadChunkParam.getType()
        );
        return Result.success(null);
    }

    @Transactional
    @Operation(summary = "分块下载文件")
    @PostMapping("/downloadChunk")
    @ApiImplicitParam(dataType = "DownloadChunkParam", name = "downloadChunkParam", value = "分块下载文件", readOnly = true, required = true)
    @LogAnnotation(module = "FileController", operation = "分块下载文件")
    public void downloadChunk(@RequestBody @Valid DownloadChunkParam downloadChunkParam) {
        fileService.downloadChunk(
                response,
                downloadChunkParam.getIndex(),
                downloadChunkParam.getMd5(),
                downloadChunkParam.getChunkSize()
        );
    }

    /**
     * 合并文件
     */
    @Transactional
    @Operation(summary = "合并文件")
    @PostMapping(value = "/merge")
    @ApiImplicitParam(dataType = "MergeParam", name = "mergeParam", value = "合并视频", readOnly = true, required = true)
    @LogAnnotation(module = "FileController", operation = "合并视频")
    public Result merge(@RequestBody @Valid MergeParam mergeParam) {
        fileService.merge(
                mergeParam.getMd5(),
                null
        );
        return Result.success(null);
    }

    @Transactional
    @Operation(summary = "根据md5获得已上传文件的下标")
    @PostMapping(value = "/getUploadedList")
    @ApiImplicitParam(dataType = "GetUploadListParam", name = "getUploadListParam", value = "获得已上传的下标", readOnly = true, required = true)
    @LogAnnotation(module = "ReportController", operation = "获得已上传的下标")
    public Result<List<Integer>> getUploadedList(@RequestBody @Valid GetUploadListParam getUploadListParam) {
        List<Integer> list = fileService.getUploadedList(
                getUploadListParam.getMd5()
        );
        return Result.success(list);
    }

}
